package com.itheima.d1_thread_api;

/**
 * 目标：掌握Thread的常用方法。
 */
public class ThreadTest1 {
    public static void main(String[] args) throws InterruptedException {
        //想要获取执行main方法的线程的名称
        Thread mainThread = Thread.currentThread();
        String name1 = mainThread.getName();
        System.out.println("name1 = " + name1); //main
        mainThread.setName("主线程"); //为线程设置名称
        String name2 = mainThread.getName();
        System.out.println("name2 = " + name2); //主线程

        //构造器
        MyThread t1 = new MyThread();
        t1.start();

        new MyThread().start();
        new MyThread().start();

        new MyThread("小兮").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                //这里无法调用线程的getName获取名称，因为当前是Runnable的实现类
                Thread thread = Thread.currentThread();
                System.out.println(thread.getName() + "在运行（匿名内部类）");
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                //这里无法调用线程的getName获取名称，因为当前是Runnable的实现类
                Thread thread = Thread.currentThread();
                System.out.println(thread.getName() + "在运行（匿名内部类）");
            }
        },"匿名内部类线程").start();

        new Thread(()->{
            Thread thread = Thread.currentThread();
            System.out.println(thread.getName() + "在运行（Lambda）");
        }).start();

        new Thread(()->{
            Thread thread = Thread.currentThread();
            System.out.println(thread.getName() + "在运行（Lambda）");
        },"Lambda线程").start();
    }
}


class MyThread extends Thread {
    public MyThread() {
    }

    public MyThread(String name) {
        super(name);
    }

    @Override
    public void run() {
        //调用父类getName方法
        String name = getName();
        System.out.println(name + "在执行～");
    }
}
